//
//  CartoonLongHorizontalTableViewCell.m
//  WuLi
//
//  Created by Gollum on 16/9/23.
//  Copyright © 2016年 Gollum. All rights reserved.
//

#import "CartoonLongHorizontalTableViewCell.h"
#import "CartoonLongHorizontalCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "NetworkTool.h"
#import "PopularModel.h"
#import "PopularData.h"
#import "PopularDetailData.h"

#import "CartoonDetailLIistViewController.h"
#import "CartoonDetailTagViewController.h"

#define kCartoonLongHorizontalCollectionViewCellReuse @"CartoonLongHorizontalCollectionViewCellReuse"
@interface CartoonLongHorizontalTableViewCell()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)PopularModel *popularModel;
@end

@implementation CartoonLongHorizontalTableViewCell

- (void)awakeFromNib {
    // Initialization code
    _dataArray = [NSMutableArray array];
    [self handleData];

    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    [self.collectionView setCollectionViewLayout:flowLayout];
    //注册（xib）
    
    
    UINib *nib = [UINib nibWithNibName:@"CartoonLongHorizontalCollectionViewCell" bundle:[NSBundle mainBundle]];
    
    _collectionView.bounces = NO;
    _collectionView.scrollEnabled = YES;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.userInteractionEnabled = YES;
//    _collectionView.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/256.0 green:arc4random_uniform(255)/256.0 blue:arc4random_uniform(255)/256.0 alpha:1];
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:kCartoonLongHorizontalCollectionViewCellReuse];
}

- (void)handleData{

    
    NSString *urlString = @"http://dajiaochong.517w.com/dacu_app/app/?c=MainRank&a=get_label_rank&userid=0&label=0&get_type=0&start=0&home=1&ui=0&ui_id=0";
    NSDictionary *header = @{@"User-Agent":@"kidstone.cn/2.3.0/10/iPad/iOS 9.3.2/iPad/AC4A678F-C131-43C6-9024-873D3BD1A130/kidstone"};
    
    [NetworkTool GET:urlString andBody:nil andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
        
        _popularModel = [PopularModel modelWithDic:result];
        [_dataArray addObject:_popularModel];
        
        [_collectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];









}







- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CartoonLongHorizontalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCartoonLongHorizontalCollectionViewCellReuse forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    NSURL *url = [NSURL URLWithString:_popularModel.data.data[indexPath.row].thumb_3];
    [cell.mainImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default_image"]];
    cell.titleLabel.text = _popularModel.data.data[indexPath.row].author;
    cell.contentLabel.text = _popularModel.data.data[indexPath.row].title;
    cell.num.text = _popularModel.data.data[indexPath.row].rank_id;
    return cell;
    
}



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake([UIScreen mainScreen].bounds.size.width / 3.5, [UIScreen mainScreen].bounds.size.width / 3.5 * 1.3);
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

//    UIViewController *view = [UIViewController new];




}







- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
